<template>
  <div>
    <basic-page-vxe
      ref="basePage"
      :columns="basicData.columns"
      :search-form="basicData.searchForm"
      :search-handle="basicData.searchHandle"
      list-api="queryAppClient"
      del-api="deleteAppClient"
    />
    <!--新增-->
    <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="1100px">
      <el-form ref="dataForm" :rules="rules" :model="temp" :inline="false" label-position="right" label-width="120px" style="width: 100%">
        <el-form-item label="应用编码" prop="clientCode">
          <el-input v-model="temp.clientCode" placeholder="请输入" maxlength="32" clearable :disabled="dialogStatus === 'update'" />
        </el-form-item>
        <el-form-item label="应用密钥" prop="clientSecret">
          <el-input v-model="temp.clientSecret" placeholder="请输入" maxlength="32" clearable />
        </el-form-item>
        <el-form-item label="授权类型" prop="authType">
          <el-input v-model="temp.authType" placeholder="请输入" maxlength="32" clearable />
        </el-form-item>
        <el-form-item label="授权范围" prop="authScope">
          <el-input v-model="temp.authScope" placeholder="请输入" maxlength="32" clearable />
        </el-form-item>
        <el-form-item label="令牌秒数" prop="tokenSeconds">
          <el-input-number v-model="temp.tokenSeconds" placeholder="请输入" maxlength="32" clearable />
        </el-form-item>
        <el-form-item label="刷新秒数" prop="refreshSeconds">
          <el-input-number v-model="temp.refreshSeconds" placeholder="请输入" maxlength="32" clearable />
        </el-form-item>
        <el-form-item label="回调地址" prop="fallbackUrl">
          <el-input v-model="temp.fallbackUrl" placeholder="请输入" maxlength="32" clearable />
        </el-form-item>
        <el-form-item label="应用描述" prop="clientDescription">
          <el-input v-model="temp.clientDescription" type="textarea" placeholder="请输入" maxlength="32" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addAppClient, updateAppClient } from '@/api/admin'
import { mapGetters } from 'vuex'
export default {
  name: '',
  components: {},
  data() {
    return {
      basicData: {
        columns: [
          { config: { title: '应用编码', field: 'clientCode' }},
          { config: { title: '应用密钥', field: 'clientSecret' }},
          { config: { title: '授权类型', field: 'authType' }, type: 'copy' },
          { config: { title: '授权范围', field: 'authScope' }},
          { config: { title: '令牌秒数', field: 'tokenSeconds' }},
          { config: { title: '刷新秒数', field: 'refreshSeconds' }},
          { config: { title: '回调地址', field: 'fallbackUrl' }, type: 'copy' },
          { config: { title: '应用描述', field: 'clientDescription' }, type: 'copy' },
          { config: { title: '日期', field: 'createTime' }, type: 'time' },
          { config: { title: '操作', field: 'opt', width: '180' },
            type: 'render',
            render: (h, scope) => {
              return (
                <div>
                  <el-button plain size='mini' type='primary' v-permission="'update'" onClick={() => this.handleUpdate(scope.row)}><i class='el-icon-edit'></i> 编辑</el-button>
                  <el-button plain size='mini' type='danger' v-permission="'delete'" onClick={() => this.$refs.basePage.handleDelete(scope.row)}><i class='el-icon-delete'></i> 删除</el-button>
                </div>
              )
            }
          }
        ],
        searchForm: [
          { type: 'Input', label: '应用编码', prop: 'clientCode', width: '180px' },
          { type: 'Input', label: '授权类型', prop: 'authType', width: '180px' }
        ],
        searchHandle: [
          { label: '新增', type: 'success', icon: 'el-icon-plus', permission: 'add', handle: () => { this.handleCreate() } }
        ]
      },

      temp: {
        id: undefined,
        content: ``
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      rules: {
        clientCode: [{ required: true, message: '请输入应用编码！', trigger: ['blur', 'change'] }],
        clientSecret: [{ required: true, message: '请输入应用密钥！', trigger: 'blur' }],
        authType: [{ required: true, message: '请输入授权类型！', trigger: 'blur' }],
        authScope: [{ required: true, message: '请输入授权范围！', trigger: 'blur' }],
        tokenSeconds: [{ required: true, message: '请输入令牌秒数！', trigger: 'blur' }],
        refreshSeconds: [{ required: true, message: '请输入刷新秒数！', trigger: 'blur' }],
        fallbackUrl: [{ required: true, message: '请输入回调地址！', trigger: 'blur' }],
        clientDescription: [{ required: true, message: '请输入应用描述！', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  created() {},
  mounted() {
  },
  methods: {
    // 业务逻辑
    resetTemp() {
      this.temp = {
        id: undefined,
        content: ''
      }
    },
    handleDetail(id) {
      this.$router.push({
        name: 'prjmgtDetail',
        query: { id: id }
      })
    },
    // 新增
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addAppClient(this.temp).then(response => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '新增成功',
              type: 'success',
              duration: 2000
            })
            this.handleReset()
          })
        }
      })
    },
    // 编辑
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateAppClient(this.temp).then(response => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '编辑成功',
              type: 'success',
              duration: 2000
            })
            this.handleReset()
          })
        }
      })
    },
    // 刷新
    handleReset() {
      this.$refs.basePage.getList()
    }
  }
}
</script>

<style scoped>
.warning{
  text-align: center;
  font-size: 20px;
}
.el-icon-info{
  font-size: 30px;
  color: orange;
}
</style>
